package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.FinanceQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.FinanceDao;
import cn.org.eshow.model.Finance;
import cn.org.eshow.service.FinanceManager;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "FinanceManager", endpointInterface = "cn.org.eshow.service.FinanceManager")
@Service
public class FinanceManagerImpl extends GenericManagerImpl<Finance, Integer> implements FinanceManager {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    public FinanceManagerImpl(FinanceDao financeDao) {
        super(financeDao);
        this.financeDao = financeDao;
    }

    public FinanceManagerImpl() {
    }

    @Override
    public Page<Finance> search(FinanceQuery query) {
        return financeDao.search(query);
    }

    @Override
    public List<Finance> list(FinanceQuery query) {
        return financeDao.list(query);
    }

    @Override
    public long count(FinanceQuery query) {
        return financeDao.count(query);
    }


    @Override
    public Finance balance( Double amount) {
        Finance finance = financeDao.get(1);
        finance.setUpdateTime(new Date());
        finance.setBalance(finance.getBalance() + amount);
        financeDao.save(finance);
        return finance;
    }


    @Override
    public Finance frozen( Double amount) {
        Finance finance = financeDao.get(1);
        finance.setUpdateTime(new Date());
        finance.setFrozen(finance.getFrozen() + amount);
        financeDao.save(finance);
        return finance;
    }

    @Override
    public Finance check(FinanceQuery query) {
        List<Finance> list = financeDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Finance increase(Double price) {
        Finance finance = financeDao.get(1);
        finance.setUpdateTime(new Date());
        finance.setBalance(finance.getBalance() == null ? price : (finance.getBalance() + price));
        finance.setTurnover(finance.getTurnover() == null ? price : (finance.getTurnover() + price));
        finance.setYearTurnover(finance.getYearTurnover() == null ? price : (finance.getYearTurnover() + price));
        finance.setMonthTurnover(finance.getMonthTurnover() == null ? price : (finance.getMonthTurnover() + price));
        finance.setTodayTurnover(finance.getTodayTurnover() == null ? price : (finance.getTodayTurnover() + price));
        finance = financeDao.save(finance);
        return finance;
    }



}
